package com.example.weather_pro

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.ArrayAdapter
import android.widget.ImageView
import android.widget.ListView
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import com.example.weather_pro.weather.Forecast
import com.example.weather_pro.weather.Weather
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken

class MainActivity2 : AppCompatActivity() {
    val baseurl = "http://t.weather.itboy.net/api/weather/city/"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main2)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }


        val cityCode = intent.getStringExtra("city_code")
        val queue = Volley.newRequestQueue(this)
        val stringRequest = StringRequest(baseurl+cityCode,{
            val gson = Gson()
            val WaetherType = object :TypeToken<Weather>(){}.type
            val weather = gson.fromJson<Weather>(it,WaetherType)

            var text_city = findViewById<TextView>(R.id.textView)
            var text_pro = findViewById<TextView>(R.id.textView4)
            var text_wendu = findViewById<TextView>(R.id.text_wendu)
            var text_shidu = findViewById<TextView>(R.id.text_shidu)
            var image = findViewById<ImageView>(R.id.imageView)

            text_city.text = weather.cityInfo.city
            text_pro.text = weather.cityInfo.parent
            text_wendu.text = weather.data.wendu
            text_shidu.text = weather.data.shidu

            val firstday = weather.data.forecast.first()
            when(firstday.type){
                "晴" -> image.setImageResource(R.drawable.sun)
                "阴" -> image.setImageResource(R.drawable.mcloud)
                "多云" -> image.setImageResource(R.drawable.cloud)
                "雨" -> image.setImageResource(R.drawable.rain)
                "雪" -> image.setImageResource(R.drawable.snow)
                else -> image.setImageResource(R.drawable.thunder)
            }
            val adapter = ArrayAdapter<Forecast>(this,android.R.layout.simple_list_item_1,weather.data.forecast)
            var listview = findViewById<ListView>(R.id.listView6)
            listview.adapter = adapter


            Log.d("syj","${weather.cityInfo.city} ${weather.cityInfo.parent}")
        },{
            Log.d("syj","$it")
        })
        queue.add(stringRequest)
    }
}